<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">入库作业单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="workNo" class="layui-input" type="text" placeholder="请输入入库作业单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button type="button" class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button type="button" id="purchaseStorageWorkBtnAdd" class="layui-btn icon-btn color-reseda">添加入库作业单</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>

            <!-- 表格 -->
            <table class="layui-table" id="purchaseStorageWorkTable" lay-filter="purchaseStorageWorkTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseStorageWorkTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="view">查看</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="purchaseStorageWorkForm">
    <form lay-filter="purchaseStorageWorkForm" class="layui-form model-form">
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购订单</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select id="purchaseOrderId" name="purchaseOrderId" lay-filter="purchaseOrderId" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="businessContact" class="layui-input"/>
                </div>
            </div>
        </div>

        <input name="warehouseId" type="hidden"/>
        <div style="margin-left: 30px;">
            <table class="layui-table">
                <thead>
                <tr style="">
                    <td style="width: 150px; text-align: center;">采购入库签收编号</td>
                    <td style="width: 120px; text-align: center;">供应商联系人</td>
                    <td style="width: 120px; text-align: center;">采购部门</td>
                    <td style="width: 120px; text-align: center;">采购申请人</td>
                    <td style="width: 120px; text-align: center;">订单审核人</td>
                    <td style="width: 120px; text-align: center;">运输方式</td>
                    <td style="width: 150px; text-align: center;">运单编号</td>
                    <td style="width: 150px; text-align: center;" id="variable">车牌号</td>
                    <td style="width: 120px; text-align: center;">电话</td>
                    <td style="width: 120px; text-align: center;">入库仓库</td>
                </tr>
                </thead>
                <tbody>
                <tr class="layui-form componentTr" >
                    <td id="signNo" class="typeTd">
                        <select name="purchaseSignForId" lay-filter="getPurchaseSignFor" lay-verify="required" required></select>
                    </td>
                    <td id="businessContact" style="text-align: center;"></td>
                    <td id="departmentName" style="text-align: center;"></td>
                    <td id="purchaseApplicant" style="text-align: center;"></td>
                    <td id="auditorName" style="text-align: center;"></td>
                    <td id="transportModeDesc" style="text-align: center;"></td>
                    <td id="waybillNo" style="text-align: center;"></td>
                    <td id="plateNo" style="text-align: center;"></td>
                    <td id="phone" style="text-align: center;"></td>
                    <td id="warehouseName" style="text-align: center;"></td>
                </tr>
                </tbody>
            </table>

            <div class="layui-btn" id="addProduct" style="margin-top: 10px;">添加入库作业物料</div>

            <div id="productList">
                <table class="layui-table" id="purchaseStorageWorkProductTable" lay-filter="purchaseStorageWorkProductTable"></table>
            </div>
        </div>

        <div class="layui-form-item text-center" style="margin-top: 50px;">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="purchaseStorageWorkFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>


<!--入库作业物料-->
<script type="text/html" id="purchaseStorageWorkProductList">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料编号：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料名称：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="productName" class="layui-input" type="text" placeholder="请输入物料名称"/>
                                        </div>
                                    </div>
                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn color-green" lay-filter="deliveryRecordProductFormSubSearchLog" lay-submit>查询</button>
                                        <button class="layui-btn icon-btn color-white" lay-filter="deliveryRecordProductResets" lay-submit>重置</button>
                                        <button class="layui-btn layui-btn-normal icon-btn" id="confirmChoose">确认选择</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="purchaseSignForProductTable" lay-filter="purchaseSignForProductTable"></table>
            </div>
        </div>
    </div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseDeliveryRecordProductTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!-- 详情弹窗 -->
<script type="text/html" id="purchaseStorageWorkView">
    <form lay-filter="purchaseStorageWorkView" class="layui-form model-form">
        <div style="margin-left: 30px;">
            <table class="layui-table">
                <thead>
                <tr style="">
                    <td style="width: 150px; text-align: center;">采购入库签收编号</td>
                    <td style="width: 120px; text-align: center;">供应商联系人</td>
                    <td style="width: 120px; text-align: center;">采购部门</td>
                    <td style="width: 120px; text-align: center;">采购申请人</td>
                    <td style="width: 120px; text-align: center;">订单审核人</td>
                    <td style="width: 120px; text-align: center;">运输方式</td>
                    <td style="width: 150px; text-align: center;">运单编号</td>
                    <td style="width: 150px; text-align: center;" id="variable">车牌号</td>
                    <td style="width: 120px; text-align: center;">电话</td>
                    <td style="width: 120px; text-align: center;">入库仓库</td>
                </tr>
                </thead>
                <tbody>
                <tr class="layui-form componentTr" >
                    <td id="signNo" class="typeTd" style="text-align: center;"></td>
                    <td id="businessContact" style="text-align: center;"></td>
                    <td id="departmentName" style="text-align: center;"></td>
                    <td id="purchaseApplicant" style="text-align: center;"></td>
                    <td id="auditorName" style="text-align: center;"></td>
                    <td id="transportModeDesc" style="text-align: center;"></td>
                    <td id="waybillNo" style="text-align: center;"></td>
                    <td id="plateNo" style="text-align: center;"></td>
                    <td id="phone" style="text-align: center;"></td>
                    <td id="warehouseName" style="text-align: center;"></td>
                </tr>
                </tbody>
            </table>

            <table class="layui-table" id="purchaseStorageWorkProductTable" lay-filter="purchaseStorageWorkProductTable"></table>
        </div>
    </form>
</script>




<style>
    .productStorageWorkDetailFormCalss .open-details-box{
        padding: 20px;
    }
    .productStorageWorkDetailFormCalss .open-details-box input{
        border: none !important;
        color: #666;
        width: auto;
    }
    .productStorageWorkDetailFormCalss .layui-form-label{
        width: 90px;
    }
    div[lay-id="workShowDetailTable"] .layui-table-cell,
    div[lay-id="workShowDetailTable"] .layui-form,
    div[lay-id="workShowDetailTable"] .layui-table-box,
    div[lay-id="workShowDetailTable"] .layui-table-body {
        overflow: visible;
    }
    .storageWorkIsSelect .layui-table-cell{
        height: auto;
    }
</style>

<!-- 添加入库作业弹窗 -->
<script type="text/html" id="storageWorkAddForm">
    <form lay-filter="storageWorkAddForm" class="layui-form model-form">
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购订单</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <select id="purchaseOrderId" name="purchaseOrderId" lay-filter="purchaseOrderId" lay-search></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="businessContact" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="businessContact" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="businessContact" class="layui-input"/>
                </div>
            </div>
        </div>

        <div class="open-details-box" style="height: calc(100% - 75px);">
            <div class="layui-row table-bor storageWorkIsSelect">
                <table class="layui-table" id="workShowDetailTable" lay-filter="workShowDetailTable"></table>
            </div>
            <div class="layui-row table-bor scroll-table-4 workBinDom" style="margin-top: 15px;display: none">
                <div class="layui-form-item">
                    <div class="open-details-title">料箱列表</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label w-auto">输入料箱编码</label>
                    <div class="layui-input-inline" style="width: 200px;">
                        <input name="storageWorkBinCode" type="text" class="layui-input workBinCode" placeholder="请输入料箱编码"/>
                    </div>
                    <div class="layui-input-inline">
                        <a class="layui-btn icon-btn color-green" id="storageWorkBinListAdd">添加料箱</a>
                    </div>
                </div>
                <table class="layui-table" id="storageWorkBinListTable" lay-filter="storageWorkBinListTable"></table>
            </div>

            <div class="layui-row table-bor scroll-table-4 spaceDom" style="margin-top: 15px;display: none">
                <div class="layui-form-item">
                    <div class="open-details-title">货位列表</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label w-auto">输入货位编码</label>
                    <div class="layui-input-inline" style="width: 200px;">
                        <input name="code" type="text" class="layui-input workBinCode" placeholder="请输入货位编码"/>
                    </div>
                    <div class="layui-input-inline">
                        <a class="layui-btn icon-btn color-green" id="addWarehouseShelfSpace">添加货位</a>
                    </div>
                </div>
                <table class="layui-table" id="warehouseShelfSpaceTable" lay-filter="warehouseShelfSpaceTable"></table>
            </div>

        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!--料箱操作按钮-->
<script type="text/html" id="storageWorkBinListTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="add">添加入库信息</a>
</script>

<!--货位操作按钮-->
<script type="text/html" id="warehouseShelfSpaceTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="add">添加入库信息</a>
</script>

<script type="text/html" id="workShowDetailTableBar">
    {{# if(d.storageCode != null){  }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="check">查询</a>
    {{# } }}
</script>

<!--入库签收单-->
<script type="text/html" id="storageCodeTemplate">
    <form lay-filter="storageCodeForm" class="layui-form model-form" style="padding: 0;">
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 240px;">
                <input name="storageCode" value="{{d.storageCode != null ? d.storageCode : ''}}" type="text" class="layui-input" placeholder="请输入签收单号"/>
            </div>
            <div class="layui-input-inline" style="margin-left: 5px;">
                <a class="layui-btn icon-btn color-green" id="storageCodeConfig">确定</a>
            </div>
        </div>
    </form>
</script>

<!--物料编号-->
<script type="text/html" id="storageProductTemplate">
    <form lay-filter="storageProductForm" class="layui-form model-form" style="padding: 0;">
        <select name="storageProductSku" id="storageProductSku" lay-filter="storageProductSku" lay-verify="required">
            <option value="">请选择入库物料</option>
        </select>
    </form>
</script>

<!--选择仓库-->
<script type="text/html" id="warehouseTemplate">
    <form lay-filter="warehouseTemplateForm" class="layui-form model-form" style="padding: 0;">
        <select name="storageWarehouse" id="storageWarehouse" lay-filter="storageWarehouse" lay-verify="required">
        </select>
    </form>
</script>


<!--入库信息弹窗-->
<script type="text/html" id="storageInfoAddForm">
    <form lay-filter="storageInfoAddForm" class="layui-form model-form" style="padding: 5px 30px 0 30px;height: calc(100% - 5px);overflow: hidden;">
        <div class="open-details-box" style="height: calc(100% - 75px);">
            <table class="layui-table" id="storageProductTable" lay-filter="storageProductTable"></table>

            <div class="layui-row table-bor isSelectCode" style="margin-top: 15px;">
                <div class="layui-form-item">
                    <label class="layui-form-label w-auto">输入包装码</label>
                    <div class="layui-input-inline" style="width: 260px;">
                        <input name="workLabelCode" type="text" class="layui-input workBinCode" placeholder="请输入包装码" style="width: 100%;"/>
                    </div>
                    <div class="layui-input-inline">
                        <button class="layui-btn icon-btn color-green" id="addWorkLabelCode">添加签收信息</button>
                    </div>
                </div>
                <div class="isSignLabelCode scroll-table-4" style="display: none" id="productList">
                    <table class="layui-table" id="storageInfoListTable" lay-filter="storageInfoListTable"></table>
                </div>
            </div>
            <div class="layui-row isSignLabelCode" style="display: none;margin-top: 15px;">
                <div class="layui-form-item">
                    <label class="layui-form-label">入库作业人</label>
                    <div class="layui-input-inline">
                        <input name="signStationName" placeholder="请输入入库作业人" type="text" class="layui-input" readonly="readonly"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">作业说明</label>
                    <div class="layui-input-inline">
                        <input name="remark" placeholder="请输入入库作业说明" type="text" class="layui-input"/>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="storageInfoAddFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>


<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var storageName = '', skuCode = ''; // 入库类型

        // 渲染表格
        var insTb = table.render({
            elem: '#purchaseStorageWorkTable',
            url: config.base + 'enterprise/purchase/admin/purchaseStorageWork/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'}
                ,{field: 'workNo', align:'center', title: '入库作业单编号'}
                ,{field: 'signNo', align:'center', title: '入库签收单编号'}
                ,{field: 'planNo', align:'center', title: '入库计划单编号'}
                ,{field: 'skuNum', align:'center', title: '入库作业SKU数'}
                ,{field: 'warehouseName', align:'center', title: '入库仓库'}
                ,{field: 'creatorName', align:'center', title: '操作人'}
                ,{field: 'createTime', align:'center', title: '入库时间'}
                ,{align: 'center', toolbar: '#purchaseStorageWorkTableBar', title: '操作', width: 120}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(purchaseStorageWorkTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'view') { // 查看
                showViewModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(purchaseStorageWorkTable)', function(obj){
            table.reload('purchaseStorageWorkTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#purchaseStorageWorkBtnAdd').click(function () {
            storageWorkAddModel();
        });

        function storageWorkAddModel() {
            storageName = '';
            skuCode = '';
            admin.open({
                type: 1,
                area: ['82%', '850px'],
                offset: ['65px','300px'],
                title: '添加采购入库作业单',
                content: $('#storageWorkAddForm').html(),
                success: function () {
                    form.render();
                    let planId = '', signId = '', warehouseId = '', hasUseBox = false, storageCode = '', warehouseName = '', signProductDetailId = ''; // 计划ID & 签收ID & 仓库ID
                    let detailData = [{}];
                    let productBoxList = [];
                    let productShelfSpaceList = [];
                    let signProductList = [];
                    let warehouseList = [];
                    // 详情列表
                    var insTb2 = table.render({
                        elem: '#workShowDetailTable',
                        data: detailData,
                        limit: detailData.limit,
                        cellMinWidth: 120,
                        page: false,
                        cols: [[
                            {field: 'storageCode', title: '采购入库签收单', templet: "#storageCodeTemplate", width: 320, align: 'center'}
                            ,{field: 'storageProduct', title: '物料编号', templet: "#storageProductTemplate", width: 260, align: 'center'}
                            ,{field: 'productTypeName', title: '物料类型', align: 'center'}
                            ,{field: 'unitName', title: '计价单位', align: 'center'}
                            ,{field: 'storageQuantity', title: '入库数量', align: 'center'}
                            ,{field: 'purchase', title: '入库仓库', templet: "#warehouseTemplate", width: 220, align: 'center'}
                            ,{field: 'tableBar', align: 'center', toolbar: '#workShowDetailTableBar', title: '操作', width: 120}
                        ]],
                        done: function() {

                            // 确定入库签收单
                            $("#storageCodeConfig").click(function () {
                                storageCode = $("input[name='storageCode']").val();
                                if(storageCode == '') {
                                    layer.msg('请输入采购签收单号', { icon: 2 });
                                    return false;
                                }
                                getDeliverySignInfo();
                            });
                        }
                    });

                    // 查询采购入库签收单下的物料
                    function getDeliverySignInfo() {
                        layer.load(2);
                        admin.req('enterprise/purchase/admin/purchaseSignFor/getDeliverySignInfo', {signNo: storageCode}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                signProductList = res.data.signProductList;
                                warehouseName = res.data.warehouseName;
                                warehouseId = res.data.warehouseId;
                                signId = res.data.id;
                                planId = res.data.planId;
                                loadSelectHtml(signProductList, '请选择入库物料', 'storageProductSku', 1)
                                layer.msg("查询成功，请选择入库物料", { icon: 1 });
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'GET');
                    }

                    // 选择物料
                    form.on('select(storageProductSku)', function (d) {
                        skuCode = d.elem[d.elem.selectedIndex].text;
                        signProductDetailId = d.value;
                        warehouseId = '';
                        $(".spaceDom").hide();
                        $(".workBinDom").hide();
                        getSignPackLabelNum(signProductDetailId);
                    });

                    // 查询签收单物料信息和签收的条码总数
                    function getSignPackLabelNum(id) {
                        layer.load(2);
                        admin.req('enterprise/purchase/admin/purchaseSignFor/getSignPackLabelNum', {signProductDetailId: signProductDetailId}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                hasUseBox = res.data.hasUseBox;
                                let dataS = [{
                                    storageCode: storageCode,
                                    unitName: res.data.unitName,
                                    productTypeName: res.data.productTypeName,
                                    storageQuantity: res.data.storageQuantity,
                                    warehouseName: warehouseName,
                                }]
                                detailData = dataS;
                                table.reload('workShowDetailTable', {
                                    data: dataS,
                                    limit: dataS.length
                                });
                                // 重新赋值
                                loadSelectHtml(signProductList, '请选择入库物料', 'storageProductSku', 1, id);

                                if(id != ""){
                                    admin.req('enterprisecab/warehouse/admin/warehouse/list', {productTypeId: res.data.productTypeId, limit: 9999}, function (res) {
                                        layer.closeAll('loading');
                                        if (res.code == 200) {
                                            loadSelectHtml(res.data, '请选择仓库', 'storageWarehouse', 2);
                                            // 选择仓库
                                            form.on('select(storageWarehouse)', function (d) {
                                                warehouseName = d.elem[d.elem.selectedIndex].text;
                                                warehouseId = d.value;
                                            });
                                        } else {
                                            layer.msg(res.msg, { icon: 2 });
                                        }
                                    }, 'GET');
                                }
                                form.render('select');
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'GET');
                    }

                    // 工具条点击事件
                    table.on('tool(workShowDetailTable)', function (obj) {
                        var data = obj.data;
                        if(warehouseId == '') {
                            layer.msg('请选择仓库', { icon: 2 });
                            return false;
                        }
                        if (obj.event === 'check') {
                            if(hasUseBox){
                                $(".spaceDom").hide();
                                $(".workBinDom").show();
                                getPLanProductBoxByJobProductId();
                            }else{
                                $(".spaceDom").show();
                                $(".workBinDom").hide();
                                getPLanProductShelfSpaceByJobProductId();
                            }
                        }
                    });

                    // 查询物料下的料箱
                    function getPLanProductBoxByJobProductId() {
                        layer.load(2);
                        admin.req('enterprisecab/order/admin/storageWork/getPLanProductBoxByJobProductId', { skuCode: skuCode, warehouseId: warehouseId}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                productBoxList = res.data;
                                productBoxList.forEach((item, index) => {
                                    productBoxList[index].storageQuantity = 0;
                                })
                                table.reload('storageWorkBinListTable', {
                                    data: productBoxList,
                                    limit: productBoxList.length
                                });
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'GET');
                    }

                    // 扫描料箱
                    $("#storageWorkBinListAdd").click(function() {
                        let code = $("input[name='storageWorkBinCode']").val();
                        if(code == '') {
                            layer.msg('请输入料箱编码', { icon: 2 });
                            return false;
                        }
                        let filterList = productBoxList.filter((item) => {
                            return item.code == code;
                        });

                        if (filterList.length > 0) {
                            layer.msg('料箱编码已存在', {icon: 2});
                            return false;
                        }
                        getPalletBox(code);
                    });

                    // 扫描条码添加料箱
                    function getPalletBox(code) {
                        layer.load(2);
                        admin.req('enterprise/purchase/admin/purchaseSignFor/getPalletBox', {code: code, warehouseId: warehouseId}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                let resList = res.data;
                                resList.storageQuantity = 0;

                                productBoxList.push(resList);

                                // 渲染表格
                                table.reload('storageWorkBinListTable', {
                                    data: productBoxList,
                                    limit: productBoxList.length
                                });
                                $("input[name='storageWorkBinCode']").val('')
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'GET');
                    }

                    // 料箱列表
                    var storageWorkBinListTable = table.render({
                        elem: '#storageWorkBinListTable',
                        data: [],
                        cellMinWidth: 100,
                        cols: [[
                            { field: 'number', title: '序号', width: 50, type: 'numbers' }
                            ,{field: 'code', title: '料箱编号', align: 'center'}
                            ,{field: 'areaName', title: '所在库区', align: 'center'}
                            ,{field: 'spaceCode', title: '所在货位', align: 'center'}
                            ,{field: 'labelQuantity', title: '原始包装量', align: 'center'}
                            ,{field: 'amountQuantity', title: '原始最小单位量', align: 'center'}
                            ,{field: 'maximumLoad', title: '最大载重', align: 'center'}
                            ,{field: 'storageQuantity', title: '新增最小单位量', align: 'center'}
                            ,{field: 'updateTime', title: '最后变动时间', align: 'center'}
                            ,{align: 'center', toolbar: '#storageWorkBinListTableBar', title: '操作'}
                        ]]
                    });

                    // 工具条点击事件
                    table.on('tool(storageWorkBinListTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'add') {
                            storageSignProductModel(data.id, signId, planId, data.code, warehouseId, signProductDetailId, true);
                        }
                    });

                    // 子页面刷新父页面
                    window.valueSignModel = function (valueSignMap) {
                        let signValue = 0;
                        valueSignMap.purchaseStorageWorkProducts.forEach((item, index) => {
                            signValue += parseInt(item.quantity);
                        });
                        if(valueSignMap.hasBox){
                            productBoxList.forEach((item, index) => {
                                // 判断是否添加过条码
                                if(item.code ==  valueSignMap.boxCode) {
                                    if(productBoxList[index].storageQuantity > 0) {
                                        productBoxList[index].storageQuantity = parseInt(signValue) + parseInt(item.storageQuantity);
                                    } else {
                                        productBoxList[index].storageQuantity = signValue
                                    }
                                }
                            });
                            table.reload('storageWorkBinListTable', {
                                data: productBoxList,
                                limit: productBoxList.length
                            });
                        }else{
                            productShelfSpaceList.forEach((item, index) => {
                                // 判断是否添加过条码
                                console.log(item.code);
                                console.log(valueSignMap.boxCode);
                                console.log(item.code ==  valueSignMap.boxCode);
                                if(item.code ==  valueSignMap.boxCode) {
                                    console.log(productShelfSpaceList[index].storageQuantity);
                                    console.log(signValue);
                                    if(productShelfSpaceList[index].storageQuantity > 0) {
                                        productShelfSpaceList[index].storageQuantity = parseInt(signValue) + parseInt(item.storageQuantity);
                                    } else {
                                        productShelfSpaceList[index].storageQuantity = signValue
                                    }
                                }
                            });
                            table.reload('warehouseShelfSpaceTable', {
                                data: productShelfSpaceList,
                                limit: productShelfSpaceList.length
                            });
                        }
                    }



                    // 查询物料下的货位
                    function getPLanProductShelfSpaceByJobProductId() {
                        layer.load(2);
                        admin.req('enterprisecab/warehouse/admin/warehouseShelfSpace/getShelfSpace',
                            {skuCode: skuCode, warehouseId: warehouseId}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                productShelfSpaceList = res.data;
                                productShelfSpaceList.forEach((item, index) => {
                                    productShelfSpaceList[index].storageQuantity = 0;
                                })
                                table.reload('warehouseShelfSpaceTable', {
                                    data: productShelfSpaceList,
                                    limit: productShelfSpaceList.length
                                });
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'GET');
                    }

                    // 扫描货位
                    $("#addWarehouseShelfSpace").click(function() {
                        let code = $("input[name='code']").val();
                        if(code == '') {
                            layer.msg('请输入货位编码', { icon: 2 });
                            return false;
                        }
                        let filterList = productShelfSpaceList.filter((item) => {
                            return item.code == code;
                        });

                        if (filterList.length > 0) {
                            layer.msg('货位编码已存在', {icon: 2});
                            return false;
                        }
                        getShelfSpace(code);
                    });

                    // 扫描条码添加货位
                    function getShelfSpace(code) {
                        layer.load(2);
                        admin.req('enterprise/purchase/admin/purchaseSignFor/getShelfSpace', {code: code, warehouseId: warehouseId}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                let resList = res.data;
                                resList.storageQuantity = 0;

                                productShelfSpaceList.push(resList);

                                // 渲染表格
                                table.reload('warehouseShelfSpaceTable', {
                                    data: productShelfSpaceList,
                                    limit: productShelfSpaceList.length
                                });
                                $("input[name='code']").val('')
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'GET');
                    }

                    // 货位列表
                    var warehouseShelfSpaceTable = table.render({
                        elem: '#warehouseShelfSpaceTable',
                        data: [],
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 50, type: 'numbers' }
                            ,{field: 'warehouseAreaName', title: '所在库区', align: 'center'}
                            ,{field: 'spaceCode', title: '所在货位', align: 'center'}
                            ,{field: 'labelQuantity', title: '原始包装量', align: 'center'}
                            ,{field: 'amountQuantity', title: '原始最小单位量', align: 'center'}
                            // ,{field: 'maximumLoad', title: '最大载重', align: 'center'}
                            ,{field: 'storageQuantity', title: '新增最小单位量', align: 'center'}
                            // ,{field: 'updateTime', title: '最后变动时间', align: 'center'}
                            ,{align: 'center', toolbar: '#warehouseShelfSpaceTableBar', title: '操作'}
                        ]]
                    });

                    // 工具条点击事件
                    table.on('tool(warehouseShelfSpaceTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'add') {
                            storageSignProductModel(data.id, signId, planId, data.code, warehouseId, signProductDetailId, false);
                        }
                    });

                }
            });
        }

        // 入库签收物料信息
        function storageSignProductModel(boxId, signId, planId, boxCode, warehouseId, signProductDetailId, hasBox) {
            var insTb3A =  admin.open({
                type: 1,
                area: ['70%', '850px'],
                offset: '65px',
                title: '入库签收物料',
                content: $('#storageInfoAddForm').html(),
                success: function () {

                    // 采购入库作业物料
                    table.render({
                        elem: '#storageProductTable',
                        url: config.base + 'enterprise/purchase/admin/purchaseSignForProduct/list',
                        where: {id: signProductDetailId},
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'skuCode', title: '物料编号', align: 'center'}
                            ,{field: 'productName', title: '物料名称', align: 'center'}
                            ,{field: 'hasStorageWorkQuantity', title: '已入库数量', align: 'center'}
                            ,{field: 'surplusStorageWorkQuantity', title: '剩余入库数', align: 'center', templet: function (d) {
                                    return d.signForBaseNum - d.hasStorageWorkQuantity
                                }}
                        ]]
                    });

                    let signLabelData = []; // 条码列表
                    $("input[name='signStationName']").val(config.getUser().username);

                    // 查询包装条码
                    $("#addWorkLabelCode").click(function() {
                        let code = $("input[name='workLabelCode']").val();
                        if(code == '') {
                            layer.msg('请输入包装码', { icon: 2 });
                            return false;
                        }
                        let filterSign = signLabelData.filter((item) => {
                            return item.packLabeCode == code;
                        });

                        if (filterSign .length > 0) {
                            layer.msg('包装码已存在', {icon: 2});
                            return false;
                        }
                        getSignPackLabel(code);
                        return false;
                    });

                    // 扫描条码
                    function getSignPackLabel(code) {
                        for (var i = 0; i < signLabelData.length; i++) {
                            if(signLabelData[i].packLabelCode == code){
                                layer.msg("该包装编码已添加", { icon: 2 });
                                return false;
                            }
                        }
                        layer.load(2);
                        admin.req('enterprise/purchase/admin/purchaseSignFor/getSignPackLabel', {
                            code: code,
                            signId: signId,
                            inoutType: '1',
                            signProductDetailId: signProductDetailId
                        }, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                $(".isSignLabelCode").show();
                                let napeData = res.data;
                                napeData.statusType = 'created';
                                napeData.warehouseId = warehouseId;
                                napeData.storageQuantity = napeData.packLabeQuantity;

                                signLabelData.push(napeData);
                                console.log(signLabelData);
                                // 渲染表格
                                table.reload('storageInfoListTable', {
                                    data: signLabelData,
                                    limit: signLabelData.length
                                });
                                // 清空input
                                $("input[name='workLabelCode']").val('');
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'GET');
                    }
                    var storageInfoListTable = table.render({
                        elem: '#storageInfoListTable',
                        data: signLabelData,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'packLabelCode', title: '包装编码', align: 'center', width: 300}
                            ,{field: 'planCode', title: '所属入库计划', align: 'center', width: 150}
                            ,{field: 'productName', title: '物料名称', align: 'center'}
                            ,{field: 'packName', title: '包装规格', align: 'center'}
                            ,{field: 'baseRate', title: '单位包装数', align: 'center'}
                            ,{field: 'unitName', title: '最小单位', align: 'center'}
                            ,{field: 'baseRate', title: 'SN码应收数', align: 'center'}
                            ,{field: 'quantity', title: 'SN码签收数', align: 'center', templet: function (d) {
                                    if(d.status == 'inventoryIn') {
                                        return '<span>'+ d.quantity +'</span>'
                                    } else {
                                        return '<input name="quantity" class="layui-input storageQuantity" lay-verify="required|quantity|max" style="height: 28px;" type="text" ' +
                                            'value="'+ d.quantity +'" />'
                                    }
                                }}
                            // ,{align: 'center', toolbar: '#storageInfoListTableBar', title: '操作'}
                        ]],
                        done: function () {
                            $("input[name='quantity']").on("input",function(e){
                                //获取input输入的值
                                var curValue = e.delegateTarget.value;
                                var curIndex = $(this).closest("tr").data("index");
                                if(signLabelData[curIndex]){
                                    signLabelData[curIndex].quantity = curValue;
                                }
                            });
                        }
                    });

                    form.verify({
                        quantity: [/^[1-9]\d*$/, '请输入大于0的整数'],
                        max: function (value, item) {
                            var baseRate = $(item).parent().parent().prev().find("div").text();
                            if(value > parseInt(baseRate)) {
                                return '签收数量不能大于应收数量';
                            }
                        }
                    });

                    // 工具条点击事件
                    table.on('tool(storageInfoListTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'sign') { // 签收
                            let storageQuantity = $(this).parents('td').siblings().find('.storageQuantity').val(); //签收数
                            signLabelData.forEach((item, index) => {
                                if ((data.id === item.id)) {
                                signLabelData[index].storageQuantity = storageQuantity;
                                signLabelData[index].statusType = 'sign';
                            }
                        });
                        } else if(obj.event === 'edit'){ //修改
                            signLabelData.forEach((item, index) => {
                                if ((data.id === item.id)) {
                                signLabelData[index].statusType = 'created';
                            }
                        });
                        }
                        // 渲染表格
                        table.reload('storageInfoListTable', {
                            data: signLabelData,
                            limit: signLabelData.length
                        });
                    });

                    // 表单提交
                    form.on('submit(storageInfoAddFormSubmit)', function (d) {
                        var purchaseStorageWorkProducts = [];
                        $("#productList").find("tbody tr").each(function (index, item) {
                            var purchaseStorageWorkProduct = {};
                            if($(item).find("td").length > 1){
                                $(item).find("td").each(function (tdIndex, tdItem) {
                                    purchaseStorageWorkProduct.purchaseSignForProductId = signProductDetailId;
                                    /*if($(tdItem).data("field") == "purchaseSignForProductId"){
                                        purchaseStorageWorkProduct.purchaseSignForProductId = $(tdItem).find("div").text();
                                    }*/
                                    if($(tdItem).data("field") == "packLabelCode"){
                                        purchaseStorageWorkProduct.packLabelCode = $(tdItem).find("div").text();
                                    }
                                    purchaseStorageWorkProduct.skuCode = skuCode;
                                    /*if($(tdItem).data("field") == "skuCode"){
                                        purchaseStorageWorkProduct.skuCode = $(tdItem).find("div").text();
                                    }*/
                                    if($(tdItem).data("field") == "quantity"){
                                        purchaseStorageWorkProduct.quantity = $(tdItem).find("input").val();
                                    }
                                })
                                purchaseStorageWorkProducts.push(purchaseStorageWorkProduct);
                            }
                        })
                        if(purchaseStorageWorkProducts.length > 0){
                            layer.load(2);
                            function doOk(data) {
                                //调用父页面方法
                                parent.valueSignModel(data);
                                layer.close(insTb3A);
                            }
                            d.field.purchaseSignForId = signId;
                            d.field.warehouseId = warehouseId;
                            d.field.boxCode = boxCode;
                            d.field.hasBox = hasBox;
                            d.field.purchaseStorageWorkProducts = purchaseStorageWorkProducts;
                            admin.req('enterprise/purchase/admin/purchaseStorageWork/add', JSON.stringify(d.field), function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('purchaseStorageWorkTable');
                                    layer.close(insTb3A);
                                    doOk(d.field);
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, 'POST', false, "application/json");
                        }else{
                            layer.msg("至少添加一条入库作业物料！", {icon: 2});
                        }
                        return false;
                    });
                }
            });
        }

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '80%',
                offset: '65px',
                title: data ? '修改采购入库作业' : '添加采购入库作业',
                content: $('#purchaseStorageWorkForm').html(),
                success: function () {
                    form.val('purchaseStorageWorkForm', data);

                    var codeArr = [];
                    var deliveryRecordProductList = [];

                    // 获取采购入库签收
                    admin.req('enterprise/purchase/admin/purchaseSignFor/list', {limit: 99999, filterStorageWorkComplete: 1, status: 'complete'}, function(res) {
                        if (200 == res.code) {
                            $("form select[name='purchaseSignForId']").append(new Option("请选择采购入库签收", ""));//往下拉菜单里添加元素
                            $.each(res.data, function(index, item) {
                                $("form select[name='purchaseSignForId']").append(new Option(item.signNo, item.id));//往下拉菜单里添加元素
                            })
                            form.render(); //菜单渲染 把内容加载进去
                            if(data && data.purchaseSignForId){
                                form.val('purchaseSignForForm', data);
                                setData(res, data.purchaseSignForId);
                            }
                            form.on("select(getPurchaseSignFor)", function (d) {
                                $("#productList").find("tbody").remove();
                                codeArr = [];
                                setData(res, d.value);
                            })
                        } else {
                            layer.msg('获取采购入库签收失败!', {icon : 2, time : 1000});
                        }
                    }, 'GET');

                    function setData(res, purchaseSignForId) {
                        var businessContact = "";
                        var departmentName = "";
                        var purchaseApplicant = "";
                        var auditorName = "";
                        var transportModeDesc = "";
                        var transportMode = "";
                        var waybillNo = "";
                        var plateNo = "";
                        var courierNo = "";
                        var phone = "";
                        var warehouseName = "";
                        if(purchaseSignForId != ""){
                            $.each(res.data, function(index, item) {
                                if(item.id == purchaseSignForId){
                                    businessContact = item.businessContact;
                                    departmentName = item.departmentName;
                                    purchaseApplicant = item.purchaseApplicant;
                                    auditorName = item.auditorName;
                                    transportMode = item.transportMode;
                                    transportModeDesc = item.transportModeDesc;
                                    waybillNo = item.waybillNo;
                                    plateNo = item.plateNo;
                                    courierNo = item.courierNo;
                                    phone = item.phone;
                                    warehouseName = item.warehouseName;
                                }
                            })
                        }else{
                            businessContact = "";
                            departmentName = "";
                            purchaseApplicant = "";
                            auditorName = "";
                            transportMode = "";
                            transportModeDesc = "";
                            waybillNo = "";
                            plateNo = "";
                            courierNo = "";
                            phone = "";
                            warehouseName = "";
                        }
                        $("#businessContact").text(businessContact);
                        $("#departmentName").text(departmentName);
                        $("#purchaseApplicant").text(purchaseApplicant);
                        $("#auditorName").text(auditorName);
                        $("#transportModeDesc").text(transportModeDesc);
                        $("#waybillNo").text(waybillNo);
                        if(transportMode == "courier"){
                            $("#variable").text("快递单号");
                            $("#plateNo").text(courierNo);
                        }else{
                            $("#variable").text("车牌号");
                            $("#plateNo").text(plateNo);
                        }
                        $("#phone").text(phone);
                        $("#warehouseName").text(warehouseName);
                    }



                    // 添加入库作业物料
                    $("#addProduct").click(function () {
                        var purchaseSignForId = $("form select[name='purchaseSignForId']").val();
                        if(purchaseSignForId){
                            var purchaseStorageWorkProductList = admin.open({
                                type: 1,
                                area: '80%',
                                offset: '65px',
                                title: '选择入库作业物料',
                                content: $('#purchaseStorageWorkProductList').html(),
                                success: function () {

                                    var codes = "";
                                    for (var i = 0; i < codeArr.length; i++) {
                                        if(i == 0){
                                            codes = '"'+codeArr[i]+'"';
                                        }else{
                                            codes += "," + '"'+codeArr[i]+'"';
                                        }
                                    }

                                    // 渲染表格
                                    var insTb = table.render({
                                        elem: '#purchaseSignForProductTable',
                                        url: config.base + 'enterprise/purchase/admin/purchaseSignForProduct/list',
                                        where: {purchaseSignForId: purchaseSignForId, filterStorageWorkComplete: 1, codes: codes},
                                        page: false,
                                        cellMinWidth: 100,
                                        cols: [[
                                            {type: 'checkbox'},
                                            {field: 'id', align: 'center', title: 'ID', hide: 'true'},
                                            {field: 'skuCode', align: 'center', title: '物料编号',width:120},
                                            {field: 'productName', align: 'center', title: '物料名称',width:120},
                                            {field: 'specifications', align: 'center', title: '规格型号',width:120},
                                            {field: 'quantity', align: 'center', title: '数量'},
                                            {field: 'unitName', align: 'center', title: '计量单位',width:100},
                                            {field: 'rate', align: 'center', title: '税率(%)'},
                                            {field: 'taxPrice', align: 'center', title: '含税单价'},
                                            {field: 'price', align: 'center', title: '单价'},
                                            {field: 'amount', align: 'center', title: '金额'},
                                            {field: 'tax', align: 'center', title: '税额'},
                                            {field: 'totalAmount', align: 'center', title: '合计金额'}
                                        ]]
                                    });

                                    // 搜索
                                    form.on('submit(deliveryRecordProductFormSubSearchLog)', function (data) {
                                        insTb.reload({where: data.field,page: {curr: 1}},'data');
                                    });

                                    // 重置搜索
                                    form.on('submit(deliveryRecordProductResets)', function (data) {
                                        $(".lay-header").find('select').val("");
                                        $(".lay-header").find('input').val("");
                                        let field = data.field;
                                        for (let key in field) {
                                            field[key] = ''
                                        }
                                        insTb.reload({where: field, page: {curr: 1}}, 'data');
                                    });

                                    // 确认选择
                                    $('#confirmChoose').click(function () {
                                        var checkStatus = table.checkStatus('purchaseSignForProductTable'); //test即为基础参数id对应的值
                                        var dataS = checkStatus.data;
                                        for (var i = 0; i < dataS.length; i++) {
                                            dataS[i].purchaseSignForProductId = dataS[i].id;
                                        }
                                        if (dataS.length == 0) {
                                            layer.msg("请选择物料", {icon: 2, time: 1000});
                                        }else{
                                            for (var i = 0; i < dataS.length; i++) {
                                                deliveryRecordProductList.push(dataS[i]);
                                                codeArr.push(dataS[i].skuCode);
                                            }
                                            layer.close(purchaseStorageWorkProductList);

                                            // 获取数据集合并传入表格中
                                            table.reload('purchaseStorageWorkProductTable', {
                                                data: deliveryRecordProductList,
                                                limit: deliveryRecordProductList.length
                                            });
                                        }
                                    });
                                }
                            });
                        }else{
                            layer.msg('请选择采购入库签收!', {icon : 2, time : 1000});
                        }
                    })

                    if(data){
                        // 获取签收物料
                        admin.req('enterprise/purchase/admin/purchaseSignForProduct/list', {limit: 99999, purchaseSignForId: data.id}, function(res) {
                            if (200 == res.code) {
                                deliveryRecordProductList = res.data;
                                for (var i = 0; i < deliveryRecordProductList.length; i++) {
                                    deliveryRecordProductList[i].purchaseSignForId = deliveryRecordProductList[i].id;
                                    codeArr.push(deliveryRecordProductList[i].skuCode);
                                }
                                table.reload('purchaseDeliveryRecordProductTable', {
                                    data: deliveryRecordProductList,
                                    limit: deliveryRecordProductList.length
                                });
                            } else {
                                layer.msg('获取签收物料失败!', {icon : 2, time : 1000});
                            }
                        }, 'GET');
                    }

                    // 渲染表格
                    var insTb = table.render({
                        elem: '#purchaseStorageWorkProductTable',
                        data: [deliveryRecordProductList],
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', height: 50}
                            ,{field: 'purchaseSignForProductId', title: '采购入库签收物料ID', hide: 'true'}
                            ,{field: 'productTypeId', title: '物料类型ID', hide: 'true'}
                            ,{field: 'productPic', title: '物料图片', templet: function (d) {
                                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>'
                                }}
                            ,{field: 'skuCode', title: '物料编号', width: 120}
                            ,{field: 'specifications', title: '规格号'}
                            ,{field: 'productTypeName', title: '物料类型'}
                            ,{field: 'unitName', title: '计价单位'}
                            // ,{field: 'purchaseQuantity', title: '采购数量'}
                            // ,{field: 'quantity', title: '发货数量'}
                            // ,{field: 'hasSignQuantity', title: '已签收数'}
                            // ,{field: 'surplusSignQuantity', title: '剩余签收数', templet: function (d) {
                            //         return d.quantity - d.hasSignQuantity;
                            //     }}
                            // ,{field: '', title: '是否包装', width: 120, templet: '#hasPack'}
                            ,{field: 'productPackName', align:'center', title: '包装方式', width: 120,
                                templet: '<div><input type="hidden" name="productPackId" lay-verify="productPack" value="{{d.productPackId != null ? d.productPackId : \'\'}}" /><span class="productPack" style="display: block;cursor: pointer;" data-add="{{d.productPackName != null ? false : true}}">{{d.productPackName != null ? d.productPackName : "请选择"}}</span></div>'}
                            ,{field: 'packUnit', title: '包装单位', width: 120}
                            ,{field: 'conversionRate', title: '换算率', width: 120}
                            ,{field: 'signForPackNum', title: '签收数量(按包装)', width: 140}
                            ,{field: 'signForBaseNum', title: '签收数量(按基础单位)', width: 170}
                            ,{align: 'center', toolbar: '#purchaseDeliveryRecordProductTableBar', title: '操作', width: 100, fixed: 'right'}
                        ]]
                    });

                    // 工具条点击事件
                    table.on('tool(purchaseStorageWorkProductTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'del') { // 删除
                            doDelete(obj);
                        }
                    });

                    // 删除签收物料
                    function doDelete(obj) {
                        var index = $(obj.tr).data("index");
                        layer.confirm('确定要删除吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            var skuCode = obj.data.skuCode;
                            if(codeArr.indexOf(skuCode) != -1){
                                deliveryRecordProductList.splice(index,1);
                                codeArr.splice(codeArr.indexOf(skuCode), 1);
                            }
                            table.reload('purchaseStorageWorkProductTable', {
                                data: deliveryRecordProductList,
                                limit: deliveryRecordProductList.length
                            });
                        });
                    }


                    // 表单提交事件
                    form.on('submit(purchaseStorageWorkFormSubmit)', function (d) {
                        var purchaseStorageWorkProducts = [];
                        $("#productList").find("tbody tr").each(function (index, item) {
                            var purchaseStorageWorkProduct = {};
                            if($(item).find("td").length > 1){
                                $(item).find("td").each(function (tdIndex, tdItem) {
                                    if($(tdItem).data("field") == "purchaseSignForProductId"){
                                        purchaseStorageWorkProduct.purchaseSignForProductId = $(tdItem).find("div").text();
                                    }
                                    if($(tdItem).data("field") == "skuCode"){
                                        purchaseStorageWorkProduct.skuCode = $(tdItem).find("div").text();
                                    }
                                    if($(tdItem).data("field") == "skuCode"){
                                        purchaseStorageWorkProduct.skuCode = $(tdItem).find("div").text();
                                    }
                                    if($(tdItem).data("field") == "signForBaseNum"){
                                        purchaseStorageWorkProduct.quantity = $(tdItem).find("div").text();
                                    }
                                })
                                purchaseStorageWorkProducts.push(purchaseStorageWorkProduct);
                            }
                        })
                        if(purchaseStorageWorkProducts.length > 0){
                            layer.load(2);
                            d.field.purchaseStorageWorkProducts = purchaseStorageWorkProducts;
                            admin.req(data ? 'enterprise/purchase/admin/purchaseStorageWork/update' : 'enterprise/purchase/admin/purchaseStorageWork/add', JSON.stringify(d.field), function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('purchaseStorageWorkTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, data ? 'PUT' : 'POST', false, "application/json");
                        }else{
                            layer.msg("至少添加一条入库作业物料！", {icon: 2});
                        }
                        return false;
                    });
                }
            });
        }

        // 显示详情弹窗
        function showViewModel(data) {
            admin.open({
                type: 1,
                area: ['85%','700px'],
                offset: ['150px','265px'],
                title: '采购入库作业详情',
                content: $('#purchaseStorageWorkView').html(),
                success: function () {

                    // 获取采购入库签收
                    admin.req('enterprise/purchase/admin/purchaseSignFor/list', {limit: 99999, status: 'complete'}, function(res) {
                        if (200 == res.code) {
                            if(data && data.purchaseSignForId){
                                setData(res, data.purchaseSignForId);
                            }
                        } else {
                            layer.msg('获取采购入库签收失败!', {icon : 2, time : 1000});
                        }
                    }, 'GET');

                    function setData(res, purchaseSignForId) {
                        var signNo = "";
                        var businessContact = "";
                        var departmentName = "";
                        var purchaseApplicant = "";
                        var auditorName = "";
                        var transportModeDesc = "";
                        var transportMode = "";
                        var waybillNo = "";
                        var plateNo = "";
                        var courierNo = "";
                        var phone = "";
                        var warehouseName = "";
                        if(purchaseSignForId != ""){
                            $.each(res.data, function(index, item) {
                                if(item.id == purchaseSignForId){
                                    signNo = item.signNo;
                                    businessContact = item.businessContact;
                                    departmentName = item.departmentName;
                                    purchaseApplicant = item.purchaseApplicant;
                                    auditorName = item.auditorName;
                                    transportMode = item.transportMode;
                                    transportModeDesc = item.transportModeDesc;
                                    waybillNo = item.waybillNo;
                                    plateNo = item.plateNo;
                                    courierNo = item.courierNo;
                                    phone = item.phone;
                                    warehouseName = data.warehouseName;
                                }
                            })
                        }else{
                            signNo = "";
                            businessContact = "";
                            departmentName = "";
                            purchaseApplicant = "";
                            auditorName = "";
                            transportMode = "";
                            transportModeDesc = "";
                            waybillNo = "";
                            plateNo = "";
                            courierNo = "";
                            phone = "";
                            warehouseName = "";
                        }
                        $("#signNo").text(signNo);
                        $("#businessContact").text(businessContact);
                        $("#departmentName").text(departmentName);
                        $("#purchaseApplicant").text(purchaseApplicant);
                        $("#auditorName").text(auditorName);
                        $("#transportModeDesc").text(transportModeDesc);
                        $("#waybillNo").text(waybillNo);
                        if(transportMode == "courier"){
                            $("#variable").text("快递单号");
                            $("#plateNo").text(courierNo);
                        }else{
                            $("#variable").text("车牌号");
                            $("#plateNo").text(plateNo);
                        }
                        $("#phone").text(phone);
                        $("#warehouseName").text(warehouseName);
                    }

                    // 渲染表格
                    var insTb = table.render({
                        elem: '#purchaseStorageWorkProductTable',
                        url: config.base + 'enterprise/purchase/admin/purchaseStorageWorkProduct/list',
                        where: {purchaseStorageWorkId: data.id},
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', height: 50}
                            ,{field: 'purchaseSignForProductId', title: '采购入库签收物料ID', hide: 'true'}
                            ,{field: 'productTypeId', title: '物料类型ID', hide: 'true'}
                            ,{field: 'productPic', align:'center', title: '物料图片', templet: function (d) {
                                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>'
                                }}
                            ,{field: 'skuCode', align:'center', title: '物料编号', width: 140}
                            ,{field: 'specifications', align:'center', title: '规格号', width: 140}
                            ,{field: 'productTypeName', align:'center', title: '物料类型'}
                            ,{field: 'unitName', align:'center', title: '计价单位'}
                            ,{field: 'packUnit', align:'center', title: '包装单位', width: 100}
                            ,{field: 'conversionRate', align:'center', title: '换算率', width: 100}
                            ,{field: 'signForPackNum', align:'center', title: '签收数量(按包装)', width: 140}
                            ,{field: 'signForBaseNum', align:'center', title: '签收数量(按基础单位)', width: 170}
                            ,{field: 'quantity', align:'center', title: '入库数量', width: 100}
                            ,{field: 'createTime', align:'center', title: '入库时间', width: 170}
                        ]]
                    });

                    form.val('purchaseSignForForm', data);
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/purchase/admin/purchaseStorageWork/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 导出excel
        $('#purchaseStorageWorkBtnExport').click(function () {
            var checkRows = table.checkStatus('purchaseStorageWorkTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

        /*渲染下拉框*/
        function loadSelectHtml(data, tips, htmltarget, type, showValue) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].skuCode}</option>`;
                } else if(type === 2) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                }else if(type == 6){
                    html += `<option value='${data[i].code}'>${data[i].name}</option>`;

                }
            }

            $("form select[name='" + htmltarget + "']").html(html);

            if(showValue != '') {
                $('#'+ htmltarget +' option[value=' + showValue + ']').attr('selected', true)
            }

            form.render('select');
        }

    });
</script>